package com.cos.common;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author: COS
 * @time: 2021/11/11 10:08
 * @description:
 */
public class MyFactory {
    Properties properties = new Properties();

    public MyFactory(String path) {

        InputStream is = MyFactory.class.getResourceAsStream(path);
        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Object getBean(String beanName) {
        try {
            String classPath = properties.getProperty(beanName);
            //通过反射的方式提供对象实例出去使用
            Class<?> aClass = Class.forName(classPath);
            //获取方法的实例=>使用的是默认的构造方法
            return aClass.newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return null;
    }
}
